/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.ricemine.services.identity;

import com.logiclander.ricemine.utils.LdapUtilities;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityBioDemographicsInfo;
import org.kuali.rice.kim.util.KimConstants;

import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityBioDemographicsInfoMapper implements ContextMapper {

  private final Log log = LogFactory.getLog(getClass());
  private String entityIdAttributeName = null;
  private String birthDateAttributeName = null;
  private String deceasedDateAttributeName = null;
  private String birthStateCodeAttributeName = null;
  private String cityOfBirthAttributeName = null;
  private String countryOfBirthCodeAttributeName = null;
  private String genderCodeAttributeName = null;
  private String geographicOriginAttributeName = null;
  private String maritalStatusCodeAttributeName = null;
  private String primaryLanguangeCodeAttributeName = null;
  private String secondaryLanguageCodeAttributeName = null;
  private String personalSuppressionAttributeName = null;
  private String birthDateLdapDateFormat = null;
  private String deceasedDateLdapDateFormat = null;

//beg: methods required by ContextMapper interface
  public Object mapFromContext(Object ctx) {
    validateConfiguration();
    KimEntityBioDemographicsInfo kebdi = new KimEntityBioDemographicsInfo();
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;

      // potential to be special or synthetic, so do some special processing
      if (context.getStringAttribute(entityIdAttributeName).equalsIgnoreCase("dn")) {
        kebdi.setEntityId(LdapUtilities.buildDn(context, true));
      } else if (context.getStringAttribute(entityIdAttributeName).equals("objectGUID")) {
        kebdi.setEntityId(LdapUtilities.buildGuid(context));
      } else {
        kebdi.setEntityId(context.getStringAttribute(entityIdAttributeName));
      }
      kebdi.setBirthDate(new SimpleDateFormat(birthDateLdapDateFormat).parse(LdapUtilities.materialize(context.getStringAttribute(birthDateAttributeName), "01/01/1900")));
      kebdi.setDeceasedDate(new SimpleDateFormat(deceasedDateLdapDateFormat).parse(LdapUtilities.materialize(context.getStringAttribute(deceasedDateAttributeName), "01/01/1900")));
      kebdi.setBirthStateCodeUnmasked(LdapUtilities.materialize(context.getStringAttribute(birthStateCodeAttributeName)));
      kebdi.setCityOfBirthUnmasked(LdapUtilities.materialize(context.getStringAttribute(cityOfBirthAttributeName)));
      kebdi.setCountryOfBirthCodeUnmasked(LdapUtilities.materialize(context.getStringAttribute(countryOfBirthCodeAttributeName)));
      kebdi.setGenderCodeUnmasked(LdapUtilities.materialize(context.getStringAttribute(genderCodeAttributeName)));
      kebdi.setGenderCodeUnmaskedUnmasked(LdapUtilities.materialize(context.getStringAttribute(genderCodeAttributeName)));
      kebdi.setGeographicOriginUnmasked(LdapUtilities.materialize(context.getStringAttribute(geographicOriginAttributeName)));
      kebdi.setMaritalStatusCodeUnmasked(LdapUtilities.materialize(context.getStringAttribute(maritalStatusCodeAttributeName)));
      kebdi.setPrimaryLanguageCodeUnmasked(LdapUtilities.materialize(context.getStringAttribute(primaryLanguangeCodeAttributeName)));
      kebdi.setSecondaryLanguageCodeUnmasked(LdapUtilities.materialize(context.getStringAttribute(secondaryLanguageCodeAttributeName)));
      String suppressionAttributeValue = context.getStringAttribute(personalSuppressionAttributeName);
      if (suppressionAttributeValue != null && suppressionAttributeValue.equalsIgnoreCase("TRUE")) {
        kebdi.setSuppressPersonal(true);
        kebdi.setBirthStateCode(KimConstants.RESTRICTED_DATA_MASK);
        kebdi.setCityOfBirth(KimConstants.RESTRICTED_DATA_MASK);
        kebdi.setCountryOfBirthCode(KimConstants.RESTRICTED_DATA_MASK);
        kebdi.setGenderCode(KimConstants.RESTRICTED_DATA_MASK);
        kebdi.setGeographicOrigin(KimConstants.RESTRICTED_DATA_MASK);
        kebdi.setMaritalStatusCode(KimConstants.RESTRICTED_DATA_MASK);
        kebdi.setPrimaryLanguageCode(KimConstants.RESTRICTED_DATA_MASK);
        kebdi.setSecondaryLanguageCode(KimConstants.RESTRICTED_DATA_MASK);
      } else {
        kebdi.setSuppressPersonal(false);
        kebdi.setBirthStateCode(LdapUtilities.materialize(context.getStringAttribute(birthStateCodeAttributeName)));
        kebdi.setCityOfBirth(LdapUtilities.materialize(context.getStringAttribute(cityOfBirthAttributeName)));
        kebdi.setCountryOfBirthCode(LdapUtilities.materialize(context.getStringAttribute(countryOfBirthCodeAttributeName)));
        kebdi.setGenderCode(LdapUtilities.materialize(context.getStringAttribute(genderCodeAttributeName)));
        kebdi.setGeographicOrigin(LdapUtilities.materialize(context.getStringAttribute(geographicOriginAttributeName)));
        kebdi.setMaritalStatusCode(LdapUtilities.materialize(context.getStringAttribute(maritalStatusCodeAttributeName)));
        kebdi.setPrimaryLanguageCode(LdapUtilities.materialize(context.getStringAttribute(primaryLanguangeCodeAttributeName)));
        kebdi.setSecondaryLanguageCode(LdapUtilities.materialize(context.getStringAttribute(secondaryLanguageCodeAttributeName)));
      }
    } catch (Throwable t) {
      log.error("Error in KimEntityBioDemographicsContextMapper", t);
      String msg = "KimEntityBioDemographicsContextMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    if (log.isDebugEnabled()) {
      log.debug("mapped KimEntityBioDemographics: birthDate=" + kebdi.getBirthDate());
    }
    return kebdi;
  }

//end: methods required by ContextMapper interface
//beg: helper methods
  public String getEntityIdAttributeName() {
    return this.entityIdAttributeName;
  }

  public void setEntityIdAttributeName(String attrName) {
    this.entityIdAttributeName = attrName;
  }

  public String getBirthDateAttributeName() {
    return this.birthDateAttributeName;
  }

  public void setBirthDateAttributeName(String attrName) {
    this.birthDateAttributeName = attrName;
  }

  public String getDeceasedDateAttributeName() {
    return this.deceasedDateAttributeName;
  }

  public void setDeceasedDateAttributeName(String attrName) {
    this.deceasedDateAttributeName = attrName;
  }

  public String getBirthStateCodeAttributeName() {
    return this.birthStateCodeAttributeName;
  }

  public void setBirthStateCodeAttributeName(String attrName) {
    this.birthStateCodeAttributeName = attrName;
  }

  public String getCityOfBirthAttributeName() {
    return this.cityOfBirthAttributeName;
  }

  public void setCityOfBirthAttributeName(String attrName) {
    this.cityOfBirthAttributeName = attrName;
  }

  public String getCountryOfBirthCodeAttributeName() {
    return this.countryOfBirthCodeAttributeName;
  }

  public void setCountryOfBirthCodeAttributeName(String attrName) {
    this.countryOfBirthCodeAttributeName = attrName;
  }

  public String getGenderCodeAttributeName() {
    return this.genderCodeAttributeName;
  }

  public void setGenderCodeAttributeName(String attrName) {
    this.genderCodeAttributeName = attrName;
  }

  public String getGeographicOriginAttributeName() {
    return this.geographicOriginAttributeName;
  }

  public void setGeographicOriginAttributeName(String attrName) {
    this.geographicOriginAttributeName = attrName;
  }

  public String getMaritalStatusCodeAttributeName() {
    return this.maritalStatusCodeAttributeName;
  }

  public void setMaritalStatusCodeAttributeName(String attrName) {
    this.maritalStatusCodeAttributeName = attrName;
  }

  public String getPrimaryLanguangeCodeAttributeName() {
    return this.primaryLanguangeCodeAttributeName;
  }

  public void setPrimaryLanguangeCodeAttributeName(String attrName) {
    this.primaryLanguangeCodeAttributeName = attrName;
  }

  public String getSecondaryLanguageCodeAttributeName() {
    return this.secondaryLanguageCodeAttributeName;
  }

  public void setSecondaryLanguageCodeAttributeName(String attrName) {
    this.secondaryLanguageCodeAttributeName = attrName;
  }

  public String getPersonalSuppressionAttributeName() {
    return this.personalSuppressionAttributeName;
  }

  public void setPersonalSuppressionAttributeName(String attrName) {
    this.personalSuppressionAttributeName = attrName;
  }

  public String getBirthDateLdapDateFormat() {
    return this.birthDateLdapDateFormat;
  }

  public void setBirthDateLdapDateFormat(String attrName) {
    this.birthDateLdapDateFormat = attrName;
  }

  public String getDeceasedDateLdapDateFormat() {
    return this.deceasedDateLdapDateFormat;
  }

  public void setDeceasedDateLdapDateFormat(String attrName) {
    this.deceasedDateLdapDateFormat = attrName;
  }

  private void validateConfiguration() {
    if (entityIdAttributeName == null) {
      String msg = "The property 'entityIdAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (birthDateAttributeName == null) {
      String msg = "The property 'birthDateAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (deceasedDateAttributeName == null) {
      String msg = "The property 'deceasedDateAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (birthStateCodeAttributeName == null) {
      String msg = "The property 'birthStateCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (cityOfBirthAttributeName == null) {
      String msg = "The property 'cityOfBirthAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (countryOfBirthCodeAttributeName == null) {
      String msg = "The property 'countryOfBirthCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (genderCodeAttributeName == null) {
      String msg = "The property 'genderCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (geographicOriginAttributeName == null) {
      String msg = "The property 'geographicOriginAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (maritalStatusCodeAttributeName == null) {
      String msg = "The property 'maritalStatusCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (primaryLanguangeCodeAttributeName == null) {
      String msg = "The property 'primaryLanguangeCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (secondaryLanguageCodeAttributeName == null) {
      String msg = "The property 'secondaryLanguageCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (personalSuppressionAttributeName == null) {
      String msg = "The property 'personalSuppressionAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (birthDateLdapDateFormat == null) {
      String msg = "The property 'birthDateLdapDateFormat' must be set.";
      throw new IllegalStateException(msg);
    }
    if (deceasedDateLdapDateFormat == null) {
      String msg = "The property 'deceasedDateLdapDateFormat' must be set.";
      throw new IllegalStateException(msg);
    }
  }
//end: helper methods
}
